package com.kfm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class RespController {

    @RequestMapping("/mv")
    public ModelAndView mv(){
        ModelAndView mv = new ModelAndView("hello");
    //存数据
    mv.addObject("msg","你好121");
    return mv;
    }


    @RequestMapping("/mv1")
    public ModelAndView mv1(ModelAndView mv){
        //设置视图
        mv.setViewName("hello");
        //存数据
        mv.addObject("msg","你好121");
        return mv;
    }

    @RequestMapping("mv2")
    public String mv2(Model model){
        model.addAttribute("msg","你好MV2");
        return "hello";//逻辑视图
    }

    @RequestMapping("mv3")
    public String mv3(Model model){
        model.addAttribute("msg","你好MV3");
        return "forward:/WEB-INF/views/hello.jsp"; //物理视图  也可转发到其它handler
    }



    //重定向
    @RequestMapping("/redirect")
    public String redirect(Model model, RedirectAttributes redirectAttributes){
        model.addAttribute("msg","redirect");

        redirectAttributes.addAttribute("msg","Redirect");
        return "redirect:redirect1"; // 重定向
    }

    @RequestMapping("/redirect1")
    public String redirect1(Model model,String msg){
        return "hello";
    }

    /**
     * 通过ModelAndView重定向
     * @return
     */
    @RequestMapping("/redirect2")
    public ModelAndView redirect2(){
        ModelAndView mv = new ModelAndView("redirect:redirect1");

        return mv;
    }
}
